using MediPlatform.Read.Api.Extensions;
using MediPlatform.Read.Api.Services;

namespace MediPlatform.Read.Api
{
    public class Program
    {
        public static async Task Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // 使用扩展方法添加所有服务
            builder.Services.AddAllServices(builder.Configuration);

            // 添加控制器
            builder.Services.AddControllers();
            builder.Services.AddEndpointsApiExplorer();

            var app = builder.Build();

            // 初始化数据库
            using (var scope = app.Services.CreateScope())
            {
                var databaseInitializer = scope.ServiceProvider.GetRequiredService<DatabaseInitializer>();
                await databaseInitializer.InitializeAsync();

                // 初始化退款申请测试数据
                var refundDataInitializer = scope.ServiceProvider.GetRequiredService<RefundApplicationDataInitializer>();
                await refundDataInitializer.InitializeAsync();

                // 初始化处方流转订单测试数据
                var prescriptionOrderDataInitializer = scope.ServiceProvider.GetRequiredService<PrescriptionOrderDataInitializer>();
                await prescriptionOrderDataInitializer.InitializeAsync();

                // 初始化问诊单测试数据
                var consultationOrderDataInitializer = scope.ServiceProvider.GetRequiredService<ConsultationOrderDataInitializer>();
                await consultationOrderDataInitializer.InitializeAsync();

                // 初始化挂号订单测试数据
                var registrationOrderDataInitializer = scope.ServiceProvider.GetRequiredService<RegistrationOrderDataInitializer>();
                await registrationOrderDataInitializer.InitializeAsync();

                // 初始化药品订单测试数据
                var medicalOrderDataInitializer = scope.ServiceProvider.GetRequiredService<MedicalOrderDataInitializer>();
                await medicalOrderDataInitializer.InitializeAsync();
            }

            // 配置HTTP请求管道
            if (app.Environment.IsDevelopment())
            {
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "智慧医疗平台 API v1");
                    c.RoutePrefix = "swagger"; // 设置Swagger UI为/swagger路径
                });
            }

            // 使用CORS
            app.UseCors("AllowAll");

            // 使用认证和授权
            app.UseAuthentication();
            app.UseAuthorization();

            app.MapControllers();

            await app.RunAsync();
        }
    }
}
